﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace M3G {
    public class Collisions {
        #region Field
        // 2つまとめてDictionaryにしてしまってもいいな
        // Dictionary<Node, List<Node>> nodes;
        List<Node>   colliders;
        List<List<Node>> collidees;
        #endregion

        #region Method
        public Collisions () {
            colliders = new List<Node> ();
            collidees = new List<List<Node>> ();
        }

        internal void Clear () {
            colliders.Clear ();
            collidees.Clear ();
        }

        internal void AddCollider (Node collider) {
            if (collider == null) {
                throw new ArgumentException ("Collider is null");
            }
            if (colliders.Contains (collider) == true) {
                return;
            }

            this.colliders.Add (collider);
            this.collidees.Add (new List<Node> ());
        }

        internal void AddCollidee (Node collider, Node collidee) {
            if (collider == null) {
                throw new ArgumentException ("Collider is null");
            }
            if (collidee == null) {
                throw new ArgumentException ("Collidee is null");
            }
            if (colliders.Contains (collider) == true) {
                throw new ArgumentException ("Collider does not exist in ActiveColliders");
            }

            var index = colliders.IndexOf (collider);

            this.collidees[index].Add (collidee);
        }

        public int ColliderCount {
            get { return colliders.Count; }
        }

        public Node[] ActiveColliders {
            get {
                return colliders.ToArray ();
            }
        }

        public int MaxCollideeCount {
            get {
                return collidees.Max (x => x.Count);
            }
        }


        public  Node[] Collidees (Node collider) {
            if (collider != null) {
                throw new ArgumentNullException ("Collider is null");
            }
            if (colliders.Contains (collider) == false) {
                throw new ArgumentException ("Collider does not exist in ActiveColliders");
            }

            var index    = colliders.IndexOf (collider);
            return collidees[index].ToArray ();
        }
        #endregion

    }
}
